<?php

namespace app\common\model;


use think\Model;

class Reply extends Model
{
    // 表名
    protected $name = 'Reply';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $common_where = [
        'status'=>1,
        'deletetime'=>null
    ];
    //评论人
    public function user(){
        return $this->hasOne('user','id','from_uid')->bind([
            'fromavatar'=>'avatar',
            'fromname'	=> 'nickname',
        ]);
    }

    //回复的人
    public function toUser(){
        return $this->hasOne('user','id','to_uid')->bind([
            'toavatar'=>'avatar',
            'toname'	=> 'nickname',
        ]);
//            ->setEagerlyType(0); 这个是插入数据
    }

    public function getOne($where)
    {
        $info = Reply::alias('c')->where($where)
            ->join('user u','c.from_uid = u.id','left')
            ->join('user s','c.to_uid = s.id','left')
            ->field('c.*,u.nickname fromname,u.avatar fromavatar,s.nickname tonickname')
            ->find();
        return $info;
    }
}